package com.chenglu.android.common.base;


import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewStub;
import android.view.WindowManager;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.chenglu.android.R;
import com.chenglu.android.common.base.bean.Event;
import com.chenglu.android.common.net.Constant;
import com.chenglu.android.common.utils.SPUtils;
import com.chenglu.android.common.utils.Utils;
import com.chenglu.android.mine.login.LoginActivity;
import com.chenglu.android.onelogin.OneKeyLoginActivity;
import com.gyf.immersionbar.BarHide;
import com.gyf.immersionbar.ImmersionBar;
import com.gyf.immersionbar.components.SimpleImmersionOwner;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;


import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.ButterKnife;
import butterknife.Unbinder;
import me.jessyan.autosize.internal.CustomAdapt;

/**
 * @author lyj
 * @Created by Copyright © 2023 Guangzhou Hongma Technology Co., Ltd. All rights reserved.
 * @Description：fragment的基类
 */
public abstract class BaseFragment extends Fragment {

    protected boolean isLogin = false;
    protected Context mContext;
    private ViewStub emptyView;
    private View rootView;
    private Unbinder unBinder;


    //懒加载
    protected boolean isLazyInit; //懒加载配置

    //Fragment的View加载完毕的标记
    private boolean isViewCreated;

    //Fragment对用户可见的标记
    private boolean isUIVisible;

    //懒加载数据可见
    public void setDataVisible(boolean dataVisible) {
        isDataVisible = dataVisible;
    }

    private boolean isDataVisible = false;


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = getActivity();
        //fragment 支持今天头条适配方案
//        AutoSizeConfig.getInstance().setCustomFragment(true);
    }

    @Override
    public void onResume() {
        super.onResume();
        isLogin= SPUtils.getBoolean("ISLOGIN",false);
    }


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        rootView = inflater.inflate(R.layout.layout_base, container, false);
        ((ViewGroup) rootView.findViewById(R.id.fl_content)).addView(getLayoutInflater().inflate(getLayoutId(), null));
        unBinder = ButterKnife.bind(this, rootView);
        //避免重复加载
        ViewGroup parent = (ViewGroup) rootView.getParent();
        if (parent != null) {
            parent.removeView(rootView);
        }
        if (registerEventBus()) {
            if (!EventBus.getDefault().isRegistered(this)) {
                EventBus.getDefault().register(this);
            }
        }
        return rootView;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView();
    }

    protected String  getToken(){
        String aaccessToken = SPUtils.getString(Constant.AaccessToken, null);
        if(TextUtils.isEmpty(aaccessToken)){
//            ToastUtils.show("token不能空");
            return "" ;
        }
        return aaccessToken;
    }

    //跳转登录的方法
    public void login() {

        if (Utils.checkSim(mContext)) {
            //判断是否是模拟器
            if (Utils.isEmulator()) {
                startActivity(new Intent(mContext, LoginActivity.class));
            } else {
                startActivity(new Intent(mContext, OneKeyLoginActivity.class));
            }

        } else {
            startActivity(new Intent(mContext, LoginActivity.class));
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (unBinder != null) {
            unBinder.unbind();
        }
        if (registerEventBus()) {
            EventBus.getDefault().unregister(this);
        }
    }
//***************************************空页面方法 start *************************************

    /**
     * 数据为空页面
     */
    protected void showEmptyView() {
        showEmptyView(getString(R.string.no_data));
    }

    /**
     * 数据为空页面
     *
     * @param text 显示文案
     */
    protected void showEmptyView(String text) {
        showEmptyOrErrorView(text, R.drawable.icon_error, false);
    }


    /**
     * 请求数据报错页面
     */
    protected void showErrorView() {
        showErrorView(getString(R.string.network_error_server_error));
    }

    /**
     * 请求数据报错页面
     *
     * @param text 显示文案
     */
    protected void showErrorView(String text) {
        showEmptyOrErrorView(text, R.drawable.bg_no_net, true);
    }


    /**
     * 请求数据报错或者为空页面
     *
     * @param text      提示文案
     * @param iconResId 显示的icon
     */
    public void showEmptyOrErrorView(String text, int iconResId, boolean showRefreshButton) {
        if (emptyView == null) {
            emptyView = rootView.findViewById(R.id.vs_empty);
        }
        emptyView.setVisibility(View.VISIBLE);
        rootView.findViewById(R.id.iv_empty).setBackgroundResource(iconResId);
        ((TextView) rootView.findViewById(R.id.tv_empty)).setText(text);
        View refreshButton = rootView.findViewById(R.id.tv_try_again);
        if (showRefreshButton) {
            refreshButton.setVisibility(View.VISIBLE);
            refreshButton.setOnClickListener(v -> onRefreshRetry());
        } else {
            refreshButton.setVisibility(View.GONE);
        }
    }

    /**
     * 隐藏空页面
     */
    protected void hideEmptyView() {
        if (emptyView != null) {
            emptyView.setVisibility(View.GONE);
        }
    }

    /**
     * 刷新重试
     */
    protected void onRefreshRetry() {
    }


    /**
     * 设置沉浸栏颜色
     *
     * @param statusBarColor 颜色
     */
    protected void setImmersionBarColor(int statusBarColor) {
        setImmersionBarColor(statusBarColor, false);
    }

    /**
     * 设置沉浸栏颜色
     *
     * @param statusBarColor 沉浸栏颜色
     */
    protected void setImmersionBarColor(int statusBarColor, boolean fitsSystemWindows) {
        setImmersionBarColor(statusBarColor, R.color.black, fitsSystemWindows);
    }


    /**
     * 设置沉浸栏颜色
     *
     * @param statusBarColor     沉浸栏颜色
     * @param navigationBarColor 沉浸栏图标颜色
     */
    protected void setImmersionBarColor(int statusBarColor, int navigationBarColor, boolean fitsSystemWindows) {
        ImmersionBar.with(this)
                .statusBarColor(statusBarColor)
                .navigationBarColor(navigationBarColor)
                .fitsSystemWindows(fitsSystemWindows)
                .autoDarkModeEnable(true)
                .statusBarDarkFont(true)
                .init();
    }

    /**
     * 隐藏状态栏、导航栏
     */
    protected void setImmersionBarHide() {
        ImmersionBar.with(this)
                .fitsSystemWindows(false)
                .hideBar(BarHide.FLAG_HIDE_BAR)
                .init();
    }


    //***************************************空页面方法 end*******************************//


    /**
     * 停止刷新和加载更多
     *
     * @param layout 刷新layout
     */
    protected void stopRefresh(SmartRefreshLayout layout) {
        if (layout == null) {
            return;
        }
        layout.finishLoadMore();
        layout.finishRefresh();
    }

    /**
     * 返回Fragment的根布局
     *
     * @return View
     */
    protected View getRootView() {
        return rootView;
    }


    /**
     * 子类接受事件 重写该方法
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventBus(Event event) {
    }

    /**
     * 需要接收事件 重新该方法 并返回true
     */
    protected boolean registerEventBus() {
        return false;
    }







    /**
     * 初始化视图
     *
     * @param
     */
    protected abstract void initView();

    protected abstract int getLayoutId();


    //今日头条适配方案
//    @Override
//    public boolean isBaseOnWidth() {
//        return false;
//    }
//
//    @Override
//    public float getSizeInDp() {
//        return 812;
//    }

}
